package com.atguigu05.conmunication;
/*
* 案例一使用两个线程打印1-100。线程1，线程2交替打印
* */
public class Printnumber {
    public static void main(String[] args) {
        Print print=new Print();
        Thread t1=new Thread(print);
        Thread t2=new Thread(print);
        t1.start();
        t2.start();
    }
}

class Print implements Runnable{
    private int n=1;
    @Override
    public void run() {
        while(true){
            synchronized (this) {
                notify();//wait方法放在后面就相当于执行完了我就睡。notify放在wait前面就相当于我执行完了我去叫醒一个再去睡
                if (n<=100){
                    System.out.println(Thread.currentThread().getName()+"数字"+n);
                    n++;
                    try {
                        wait();  //线程一旦进入此方法则阻塞。会释放同步监视器，sleep不会释放
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    break;
                }
            }
        }
    }
}
